#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
 
binmode STDOUT, ":utf8";
#use utf8;
 
use JSON;
require "doa_subs.pl";

use Scalar::Util qw(looks_like_number);

use CGI;
use lib '.';
use users;

my ($username,$level) = &validate;
if ($level > 5 ) { &dienice("You need Administartor Priviledge to run this");}
my $q = new CGI;
print $q->header;
print $q->start_html('User Administration');
print $q->h1('User Administration');

############################## Main Prog ##############################
#
# This program allows the activation/deactivation of users and the changing of user level
#
#


print $q->h2("This is a list of all users registered on the site");
		
my @pdata;

my $passfile = '/var/www/cgi-bin/data/passwd';
# open the password file for read
open(PASSF,"<$passfile") or &dienice("Can't open password file.");
#	flock(PASSF, LOCK_EX);          # lock the file (exclusively)
#	seek(PASSF, 0, SEEK_SET);       # rewind to beginning
my @passf = <PASSF>;            # read entire file
close(PASSF);

foreach my $i (@passf) 
{
	chomp($i);
	my ($user,$pass,$email,$level,$active,$gname) = split(/\,/,$i);
	my $user_level="a User";
	if (looks_like_number($level))
	{
		if ($level < 10 ) 
		{
			$user_level="a Super User";
		}
		if ($level < 5 ) 
		{
			$user_level="an Administrator";
		}

		my $row=$q->td($user);
		$row=$row.$q->td($email);
		$row=$row.$q->td($q->scrolling_list("list_".$user,['an Administrator','a Super User', 'a User'],[$user_level],3,'false'));
		$row=$row.$q->td($q->checkbox(-name=>"check_".$user, -checked=>$active, -value=>'Yes', -label=>'Activation'));
		$row=$row.$q->td($gname);
		push (@pdata, $row);
	}
}

print $q->start_form("POST","admin2.pl");
print $q->table({-border=>undef,-width=>'50%'},
	$q->Tr({ -align => "CENTER", -valign => "TOP" },$q->th(['User', 'Email', 'Level', 'Activated','In Game Name'])),
	$q->Tr({ -align => "CENTER", -valign => "TOP" },\@pdata));
print $q->submit('Update records','Update records');
print $q->end_form();

print $q->br();

print $q->a( {-href=>"menu.cgi"}, "Main Menu");
print $q->end_html;
